<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../js/vue.js"></script>
    <title>Document</title>
    <style>
        input { font-size:50px ;}
    </style>
</head>
<body>
    <h1> 兄弟传值</h1>
    <div id="app">
         <tom></tom>
         <jerry></jerry>
    </div>
</body>
<script>
// 路由
var hub = new Vue();

//子1
Vue.component('tom',{
    data:function(){
        return {
            num: 10
        }
    },
    template:`
    <div>
        <h1>tom=  {{num}}</h1>
        <button @click="test"> tom按钮</button>
    </div>
    `,
    methods:{
        test(){
            hub.$emit('jerryevent',10)
        }
    },
    // 生命周期
    mounted(){
        console.log( '自动执行的....tom');
        hub.$on('tomevnt',(v)=>{
            this.num+=v;
        })
    }
});

//子2
Vue.component('jerry',{
    data:function(){
        return {
            num: 80
        }
    },
    template:`
    <div>
        <h1>jerry = {{num}}</h1>
        <button @click="test"> jerry 按钮</button>
    </div>
    `,
    methods:{
        test(){
            hub.$emit('tomevnt',5)
        }
    },
    //挂载后生命周期
    mounted(){
        hub.$on('jerryevent',(v)=>{
            this.num += v
        })
    }
});

// 父
var vm = new Vue({
    el: "#app",
    methods:{
      
    }
});
</script>
</html>